package com.syj.collect.Present;

import android.util.Log;

import com.google.gson.Gson;
import com.syj.collect.base.BasePresent;
import com.syj.collect.entry.MyPhone;
import com.syj.collect.entry.MyPhotoBean;
import com.syj.collect.entry.TagAdapterData;
import com.syj.collect.entry.UserInfoBean;
import com.syj.collect.http.BaseObserver;
import com.syj.collect.http.BaseRequest;
import com.syj.collect.utils.GsonUtil;
import com.syj.collect.utils.SPUtil;
import com.syj.collect.view.CollectActivityView;
import com.syj.collect.view.PhotoActivityView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Utf8;

public class PhotoPresent extends BasePresent<PhotoActivityView> {

    public  void  getUserImgList(int page) throws UnsupportedEncodingException {

        String userstr = SPUtil.getPrefString("userInfo", "");
        Gson gson = new Gson();
        UserInfoBean.UserInfo userInfo = gson.fromJson(userstr, UserInfoBean.UserInfo.class);

        HashMap hashMap = new HashMap();
        hashMap.put("userId", userInfo.getUserId());
        hashMap.put("page", page+"");
        String str = gson.toJson(hashMap);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), str);
        BaseRequest.getInstance().getApiServise().getUserImgList(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public boolean onSuccess(String value) {
                        Gson json = new Gson();

                        MyPhone result = json.fromJson(value, MyPhone.class);
                        // JSONObject jsonObject = new JSONObject(value); MyPhone.DataDTO
//                        Log.e("tag结果 :", value);
                        // JSONArray msg = jsonObject.getJSONArray("data");
                        int code = result.getCode();
                        if (code == 0) {
                            view.initsuccess(result.getData().getList());
                        } else {
                            view.initfail(result.getMsg());
                        }

                        return false;
                    }

                    @Override
                    public void onFailure(Throwable e) throws Exception {

                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }
}
